package com.hello.sandbox.entity.pm;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Parcel;
import android.os.Parcelable;
import com.hello.sandbox.SandBoxCore;
import java.util.Objects;

/** Created by Milk on 4/20/21. * ∧＿∧ (`･ω･∥ 丶　つ０ しーＪ 此处无Bug */
public class InstalledPackage implements Parcelable {
  public int userId;
  public String packageName;

  public ApplicationInfo getApplication() {
    return SandBoxCore.getBPackageManager()
        .getApplicationInfo(packageName, PackageManager.GET_META_DATA, userId);
  }

  public PackageInfo getPackageInfo() {
    return SandBoxCore.getBPackageManager()
        .getPackageInfo(packageName, PackageManager.GET_META_DATA, userId);
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.userId);
    dest.writeString(this.packageName);
  }

  public InstalledPackage() {}

  public InstalledPackage(String packageName) {
    this.packageName = packageName;
  }

  protected InstalledPackage(Parcel in) {
    this.userId = in.readInt();
    this.packageName = in.readString();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    InstalledPackage that = (InstalledPackage) o;
    return Objects.equals(packageName, that.packageName);
  }

  @Override
  public int hashCode() {
    return Objects.hash(packageName);
  }

  public static final Parcelable.Creator<InstalledPackage> CREATOR =
      new Parcelable.Creator<InstalledPackage>() {
        @Override
        public InstalledPackage createFromParcel(Parcel source) {
          return new InstalledPackage(source);
        }

        @Override
        public InstalledPackage[] newArray(int size) {
          return new InstalledPackage[size];
        }
      };
}
